import allure,logging
import jsonpath

from utils.launch_request import launch_jdbc_Request

@allure.step("3.http响应断言")
def http_assert(case,res,index = 0):
    excepted = eval(case["Expected"])
    if case["check"]:
        #把断言的字段和预期结果变为列表
        check = eval(case["check"])
        #共同来遍历两个list
        for c,e in zip(check,excepted):
            #获取实际的结果
            result = jsonpath.jsonpath(res.json(), f"$..{c}")[index]
            logging.info(f'3.http响应断言内容：实际结果({result}) == 预期结果({e})')
            allure.attach(f'3.http响应断言内容：实际结果({result}) == 预期结果({e})",name="HTTP响应断言内容')
            assert result == e
            print(c,e)

        # result = jsonpath.jsonpath(res.json(), case["check"])[index]
        # logging.info(f'3.http响应断言内容：实际结果({result}) == 预期结果({case["Expected"]})')
        # allure.attach(f'3.http响应断言内容：实际结果({result}) == 预期结果({case["Expected"]})",name="HTTP响应断言内容')
        # assert result == case["Expected"]
    else:
        for e in excepted:
            logging.info(f'3.http响应断言内容：预期结果({e}) == 实际结果({res.text})')
            allure.attach(f'3.http响应断言内容：预期结果({e}) == 实际结果({res.text})",name="HTTP响应断言内容')
            assert str(e) in res.text



def jdbc_assert(case):
    if case["sql_check" and "sql_expected"]:
        with allure.step("4.数据库响应断言"):
            result = launch_jdbc_Request(case["sql_check"])
            logging.info(f'3.jdbc响应断言内容：预期结果({case["sql_expected"]}) == 实际结果({result})')
            allure.attach(f'3.jdbc响应断言内容：预期结果({case["sql_expected"]}) == 实际结果({result})",name="jdbc响应断言内容')

            assert result == case["sql_expected"]




